package com.jk.controller;

import com.jk.flower.mapper.FlowerRepo;
import com.jk.flower.pojo.Flower;
import com.jk.user.mapper.UserRepository;
import com.jk.user.pojo.User;
import org.springframework.ui.Model;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jk.user.pojo.Fendian;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private FlowerRepo flowerRepo;


    /***页面接口*/
    //主页面接口
    @RequestMapping("index")
    public String index(Model model){

        List<Flower> flowerList = flowerRepo.findAll();
        List<Flower> newFlower   = new ArrayList<>();
        for(int i=0; i < 4;i++){
            newFlower.add(flowerList.get(i));
        }
        model.addAttribute("flowerList",newFlower);

        return "main/mainindex";
    }

    @RequestMapping("register")
    public String register(){
        return "register";
    }

    @RequestMapping("edituser/{userid}")
    public String userEdit(@PathVariable("userid") String userId,Model model){
        User user = userRepository.findUserById(userId);
        model.addAttribute("user",user);
        return "admin/user/edit";
    }



    /***功能接口*/



    //登录接口
    @ResponseBody
    @RequestMapping("login")
    public String login(@RequestParam("userName") String userName, @RequestParam("passWord") String passWord, HttpServletRequest request){

        User user = userRepository.findUserByUserName(userName);

        HttpSession session = request.getSession(); //持久化登录
        session.setAttribute("userid",userName);
//         "userName=1&password=1"
        JSONObject result = new JSONObject();
        if(user !=null){
            if(passWord.equals(user.getUserPwd())){
                result.put("status","20000");
            }else {
                result.put("status","40000");
            }
        }
        return result.toString();
    }

    //注册用户
    @ResponseBody
    @RequestMapping("registerform")
    public String register(HttpServletRequest request, @RequestBody String json, HttpServletResponse response){
        User user = new User();

        JSONObject result = new JSONObject();

        if(request.getParameter("registerType").equals("5")){
            user.setId(UUID.randomUUID().toString().replace("-","").toLowerCase());
            user.setAddress(request.getParameter("userAddress"));
            user.setCart("");
            user.setName(request.getParameter("name"));
            user.setRealName(request.getParameter("realname"));
            user.setOrders("");
            user.setUserName(request.getParameter("userName"));
            user.setUserPwd(request.getParameter("userPassword"));
            user.setPhoneNumber(request.getParameter("phone"));
            user.setEmail(request.getParameter("userEmail"));
        }
        userRepository.save(user);
        result.put("status","20000");
        return result.toString();
    }
    //删除用户
    @ResponseBody
    @RequestMapping("delete/{userid}")
    public String deleteFlower(@PathVariable("userid")String userId){

        JSONObject res = new JSONObject();
        User user = userRepository.findUserById(userId);
        if(user != null){
            userRepository.delete(user);
        }
        res.put("status","20000");
        return res.toString();
    }

    //修改用户个人信息接口
    @ResponseBody
    @RequestMapping("update")
    public String updateUser(HttpServletRequest request,HttpServletResponse response,Model model){

        JSONObject res = new JSONObject();
        User user = new User();
        String userName = (String) request.getParameter("username");
        user = userRepository.findUserByUserName(userName);
        String pwd  = (String)request.getParameter("password");

        user.setUserPwd(pwd);
        user.setRealName((String)request.getParameter("name"));
        user.setEmail((String)request.getParameter("email"));
        user.setPhoneNumber((String)request.getParameter("phone"));
        user.setAddress((String)request.getParameter("addr"));

        res.put("status","20000");

        userRepository.save(user);

        return res.toString();
    }











}
